package com.taoym.voucher.admin.service.impl;

import com.taoym.voucher.common.constant.CommonConstant;
import com.taoym.voucher.common.enums.UserWithdrawStatusEnum;
import com.taoym.voucher.common.message.MessageDataMap;
import com.taoym.voucher.common.message.XcxMessageService;
import com.taoym.voucher.common.response.AccessTokenResp;
import com.taoym.voucher.common.utils.SecureUtils;
import com.taoym.voucher.admin.domain.XcxUserWithdraw;
import com.taoym.voucher.admin.entity.dto.XcxGetUserWithdrawsDTO;
import com.taoym.voucher.admin.entity.dto.XcxGiroDTO;
import com.taoym.voucher.admin.entity.vo.XcxGetUserWithdrawVO;
import com.taoym.voucher.admin.mapper.XcxUserInfoMapper;
import com.taoym.voucher.admin.mapper.XcxUserWithdrawMapper;
import com.taoym.voucher.admin.service.XcxUserWithdrawService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @coder taoym
 * @datetime 2021/7/16 23:22
 * @desc 小程序用户提现接口实现类
 */
@Service
public class XcxUserWithdrawServiceImpl implements XcxUserWithdrawService {

    @Resource
    private XcxUserWithdrawMapper xcxUserWithdrawMapper;
    @Resource
    private XcxUserInfoMapper xcxUserInfoMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public XcxGetUserWithdrawVO getUserWithdraws(XcxGetUserWithdrawsDTO data) {

        Integer count = xcxUserWithdrawMapper.getCount(data.getUid(), data.getStatus());

        if (count < 1) {
            return null;
        }
        List<XcxGetUserWithdrawVO.GetUserWithdraw> getUserWithdraws = xcxUserWithdrawMapper.getUserWithdraw(data.getUid(),
                data.getStatus(), (data.getPage() - 1) * data.getSize(), data.getSize());

        XcxGetUserWithdrawVO xcxGetUserWithdrawVO = new XcxGetUserWithdrawVO();
        getUserWithdraws.forEach(e -> {
            e.setStatusDesc(UserWithdrawStatusEnum.getMsg(e.getStatus()));
            e.setColor(UserWithdrawStatusEnum.getColor(e.getStatus()));
            e.setZfb(SecureUtils.decrypt(e.getZfb()));
        });
        xcxGetUserWithdrawVO.setGetUserWithdraws(getUserWithdraws);
        xcxGetUserWithdrawVO.setCount(count);

        return xcxGetUserWithdrawVO;
    }

    @Override
    public Boolean giroResult(XcxGiroDTO data) {
        XcxUserWithdraw xcxUserWithdraw = new XcxUserWithdraw();
        xcxUserWithdraw.setId(data.getId());
        xcxUserWithdraw.setStatus(data.getStatus());
        xcxUserWithdraw.setFinishTime(LocalDateTime.now());
        xcxUserWithdraw.setRemark(data.getRemark());

        Object redisObj = redisTemplate.opsForValue().get(CommonConstant.ACCESS_TOKEN_KEY);
        AccessTokenResp accessTokenResp = (AccessTokenResp) redisObj;

        String openId = SecureUtils.decrypt(xcxUserInfoMapper.getOpenId(data.getUid()));
        // 发送消息
        Map<String, Object> dataMap = MessageDataMap.getFeeWithdrawDataMap(data.getAmount(),
                UserWithdrawStatusEnum.getMsg(data.getStatus()),
                ObjectUtils.isEmpty(data.getRemark()) ? "佣金提现成功" : data.getRemark());
        Boolean send = XcxMessageService.sendXcxMessage(openId, accessTokenResp.getAccess_token(), dataMap);

        xcxUserWithdrawMapper.updateById(xcxUserWithdraw);

        return send;
    }
}
